<?php


namespace app\home\severs;


use think\facade\Db;

class StockOrderDetailSever extends Base
{
    /**
     * @autho:xiangzi
     * @auth true
     * @Date:2025/7/23
     * 更改库存状态
     */
    public static function changeSodStatus($mx=[])
    {
        foreach ( $mx as $v){
            $countNum=Db::name('stock_order_detail')->where('status',2)
                ->where('id',$v['thisId'])->value('num');
            $outNum=Db::name('stock_order_detail')->where('status',2)
                ->where('thisId',$v['thisId'])
//                ->where('type','in',[1,4])
                ->where('type','not in',[2,3,5])
                ->sum('num');
            $refoundNum=Db::name('stock_order_detail')->where('status',2)
                ->where('thisId',$v['thisId'])
                ->where('type','=',2)
                ->sum('num');
            $countNum+=$refoundNum;
            $sodStatus=1;
            if ($outNum==0){
                $sodStatus=1;
            }elseif ($outNum<$countNum){
                $sodStatus=2;
            }elseif ($outNum>=$countNum){
                $sodStatus=3;
            }
            Db::name('stock_order_detail')->where('id',$v['thisId'])->update(['sodStatus'=>$sodStatus,'update_time'=>time()]);
        }

        return ;
}

    /**
     * @param $mx
     * @param int $type 2:销售退货，4：采购退货
     * @autho:xiangzi
     * @auth true
     * @Date:2025/7/23
     *  更改退货状态
     */
    public static function changeRefundStatus($mx,$type=2)
    {

        if ($type==2){
            foreach ($mx as $v){
                $num=Db::name('stock_order_detail')->where('status',2)
                    ->where('id',$v['outThisId'])->value('num');
                $refundNum=Db::name('stock_order_detail')->where('status',2)
                    ->where('outThisId',$v['outThisId'])->where('type',$type)
                    ->sum('num');
                $refundStatus=1;
                if ($refundNum==0){
                    $refundStatus=1;
                }elseif ($num>$refundNum){
                    $refundStatus=2;
                }else if ($num<=$refundNum){
                    $refundStatus=3;
                }
                Db::name('stock_order_detail')->where('id',$v['outThisId'])->update(['refundStatus'=>$refundStatus,'update_time'=>time()]);
            }
        }else{
            foreach ($mx as $v){
                $num=Db::name('stock_order_detail')->where('status',2)
                    ->where('id',$v['thisId'])->value('num');
                $outNum=Db::name('stock_order_detail')->where('status',2)
                    ->where('thisId',$v['thisId'])->where('type',$type)
                    ->sum('num');
                $refundStatus=1;
                if ($outNum==0){
                    $refundStatus=1;
                }elseif ($num>$outNum){
                    $refundStatus=2;
                }else if ($num<=$outNum){
                    $refundStatus=3;
                }

                Db::name('stock_order_detail')->where('id',$v['thisId'])->update(['refundStatus'=>$refundStatus,'update_time'=>time()]);
            }
        }

}
}